<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程编号" prop="courseId">
        <el-input
          v-model="queryParams.courseId"
          placeholder="请输入课程编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名字" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="授课老师" prop="lecturerName">
        <el-input
          v-model="queryParams.lecturerName"
          placeholder="请输入授课老师"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:course:export']"
        >导出</el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-back"
          size="mini"
          @click="handleBack"
          v-hasPermi="['system:course:back']"
        >申请退课</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课程编号" align="center" prop="courseId" />
      <el-table-column label="课程名字" align="center" prop="courseName" />
      <el-table-column label="授课老师" align="center" prop="lecturerName" />
      <el-table-column label="学分" align="center" prop="credit" />
      <el-table-column label="时长" align="center" prop="duration" />
      <el-table-column label="开始时间" align="center" prop="startTime" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-back"
            @click="handleBack(scope.row)"
            v-hasPermi="['system:select:course:back']"
          >申请退课</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程编号" prop="courseId">
          <el-input v-model="form.courseId" placeholder="请输入课程编号" />
        </el-form-item>
        <el-form-item label="课程名字" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程名字" />
        </el-form-item>
        <el-form-item label="授课老师id" prop="lecturerId">
          <el-input v-model="form.lecturerId" placeholder="请输入授课老师id" />
        </el-form-item>
        <el-form-item label="学分" prop="credit">
          <el-input v-model="form.credit" placeholder="请输入学分" />
        </el-form-item>
        <el-form-item label="时长" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入时长" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-input v-model="form.startTime" placeholder="请输入开始时间" />
        </el-form-item>
        <el-form-item label="总计数量" prop="totalNumber">
          <el-input v-model="form.totalNumber" placeholder="请输入总计数量" />
        </el-form-item>
        <el-form-item label="已选择数量" prop="selectedNumber">
          <el-input v-model="form.selectedNumber" placeholder="请输入已选择数量" />
        </el-form-item>
        <el-form-item label="报名截止时间" prop="limitTime">
          <el-date-picker clearable
            v-model="form.limitTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择报名截止时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSelectCourse, getCourse, delCourse, addCourse, updateCourse ,selectCourse,backSelectCourse,auditCourse} from "@/api/system/course";

export default {
  name: "Select_Course",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程信息表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseId: null,
        courseName: null,
        lecturerId: null,
        credit: null,
        duration: null,
        startTime: null,
        totalNumber: null,
        selectedNumber: null,
        limitTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询课程信息列表 */
    getList() {
      this.loading = true;
      listSelectCourse(this.queryParams).then(response => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    open(row) {
      const id = row.id || this.ids
      this.$confirm('是否同意退回？', '审核', {
        distinguishCancelAndClose: true,
        confirmButtonText: '通过',
        cancelButtonText: '拒绝'
      })
        .then(() => {
          this.handleAudit(id,1)
          this.$message({
            type: 'info',
            message: '保存修改'
          });
        })
        .catch(action => {
          this.handleAudit(id,3)
        });
    },
    /** 审核按钮操作 */
    handleAudit(id,status) {
      auditCourse(id,status).then(response => {
        this.reset();
        this.message(response.data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        courseId: null,
        courseName: null,
        lecturerId: null,
        creatorId: null,
        credit: null,
        duration: null,
        startTime: null,
        totalNumber: null,
        selectedNumber: null,
        limitTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCourse(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除课程信息编号为"' + ids + '"的数据项？').then(function() {
        return delCourse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** back按钮操作 */
    handleBack(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认申请退回课程信息编号为"' + ids + '"的数据项？').then(function() {
        return backSelectCourse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("等待审核");
      }).catch(() => {});
    },
    /** 选课按钮操作 */
    handleSelect(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认选择课程信息编号为"' + ids + '"的数据项？').then(function() {
        return selectCourse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("选课成功");
      }).catch((res) => {
        console.log(res);
        this.$modal.msgError(res);
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/course/export', {
        ...this.queryParams
      }, `course_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
